package com.example.springboottest.util;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @Author LiuYiShuo
 * @DATE 2023/6/29 16:07
 * @Version 1.0
 * @Description
 */
public class ComputeUtil {


    private static final String lt = "<";
    private static final String ltE = "<=";
    private static final String gt = ">";
    private static final String gtE = ">=";
    private static final String eq = "==";

    /**
     * 比较值
     *
     * @param deviationBasis  字符串中提取
     * @param value           在比较前面的位置
     * @author liuyishuo
     * @date 2023/6/29 16:56
     * @return boolean
     **/
    public static boolean getEstimateCompute(String deviationBasis, Integer value) {

        if(ObjectUtil.isEmpty(value) && StringUtils.isEmpty(deviationBasis)){
            return false;
        }

        if (deviationBasis.contains(ltE)) {
            // 取对应的值。
            Integer intByStr = getIntByStr(deviationBasis);
            return value <= intByStr;
        }

        if (deviationBasis.contains(lt)) {
            // 取对应的值。
            Integer intByStr = getIntByStr(deviationBasis);
            return value < intByStr;
        }

        if (deviationBasis.contains(gt)) {
            // 取对应的值。
            Integer intByStr = getIntByStr(deviationBasis);
            return value > intByStr;
        }

        if (deviationBasis.contains(gtE)) {
            // 取对应的值。
            Integer intByStr = getIntByStr(deviationBasis);
            return value >= intByStr;
        }
        if (deviationBasis.contains(eq)) {
            // 取对应的值。
            Integer intByStr = getIntByStr(deviationBasis);
            return value.equals(intByStr);
        }
        return false;
    }


    public static Integer getIntByStr(String str){
        StringBuilder stringBuilder = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (Character.isDigit(c)) {
                stringBuilder.append(c);
            }
        }
        System.out.println(stringBuilder.toString());

        Integer integer = Integer.valueOf(stringBuilder.toString());

        if (ObjectUtil.isEmpty(integer)) {
            return 0;
        }
        return integer;
    }

    public static boolean getCompareValue(Integer x, String operationType,  Integer y) {

        if(ObjectUtil.isEmpty(x) || ObjectUtil.isEmpty(y)){
            return false;
        }
        if(operationType.equals(">")){
            return x > y;
        }

        if(operationType.equals(">=")){
            return   x >= y;
        }
        if(operationType.equals("<")){
            return  x < y;
        }
        if(operationType.equals("<=")){
            return   x <= y;
        }

        if(operationType.equals("==")){
            return x.equals(y);
        }
        return false;
    }


    public static int getValue(String operationType, Integer x, Integer y) {

        if(ObjectUtil.isEmpty(x) || ObjectUtil.isEmpty(y)){
            return 0;
        }
        if(operationType.equals("+")){
            return  sum(x, y);
        }

        if(operationType.equals("-")){
            return  sub(x, y);
        }
        if(operationType.equals("*")){
            return  mul(x, y);
        }
        if(operationType.equals("/")){
            return  div(x, y);
        }
        return 0;
    }

    public static int sum(int a,int b){
        System.out.println(a+b);
        return a + b;
    }
    public static int sub(int a,int b){
        System.out.println(a-b);
        return a - b;
    }
    public static int mul(int a,int b){
        System.out.println(a*b);
        return a * b;
    }
    public static int div(int a,int b){
        System.out.println(a/b);
        return a / b;
    }


    /**
     * 获取范围的计算
     *
     * @param deviationBasis 设置的检测范围，x∉[(-∞,-2.5)U(2.5,+∞)]
     * @param formulaList    作为参数传入的范围是否有区间范围有重合的。
     * @author liuyishuo
     * @date 2023/7/7 14:03
     * @return boolean
     **/
    public static boolean getRadiusCompute(String deviationBasis, List<String> formulaList) {

        List<String> iRadiusCalculation = IntervalUtil.isIRadiusCalculation(deviationBasis);

        for (String param : formulaList) {
            List<String> modelParams = IntervalUtil.getModelParams(param);
            iRadiusCalculation.addAll(modelParams);
        }

        // 为 true 是在该区间的。包含区间的值
        boolean overlap = IntervalUtil.isOverlap(iRadiusCalculation);

        //属于
        if (deviationBasis.contains("∈") && !overlap) {
            return false;
        }

        // 不属于
        if (deviationBasis.contains("∉") && overlap) {
            return false;
        }
        return true;
    }
}
